package com.test.springai.config;

import java.util.List;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 8/20/25 9:20 AM
 * @modifiedBy:
 * @version: 1.0
 */
@Configuration
public class VectorStoreConfig {
  @Bean
  VectorStore vectorStore(EmbeddingModel embeddingModel) {
    SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(embeddingModel).build();
    // 1 提取文本内容
    String filePath = "classpath:resume.txt";
    TextReader textReader = new TextReader(filePath);
    textReader.getCustomMetadata().put("filePath", filePath);
    List<Document> documents = textReader.get();
    // 2 文本切分段落
    TokenTextSplitter splitter = new TokenTextSplitter(1200, 350, 5, 100, true);
    splitter.apply(documents);
    // 3 添加
    simpleVectorStore.add(documents);
    return simpleVectorStore;
  }
}
